/***********************************************************************
 * Module:  RotateState.java
 * Author:  Nikola
 * Purpose: Defines the Class RotateState
 ***********************************************************************/

package genedit.controller;

import genedit.model.graph.GraphElement;
import genedit.view.graph.GraphWorkArea;

import java.awt.Cursor;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;

public class RotateState extends State {
	/** @param e */
	private Point2D start;
	private double globalAngle = 0;
	private GraphElement element;

	public void mouseReleased(MouseEvent e) {
		// TODO: implement
		Point2D lastPosition = e.getPoint();
		context.pointToUserSpace(lastPosition); // izvucemo ugao prethodne
												// pozicije i pozicije elementa,
												// trenutne i elementa, oduzmemo
												// ta 2 i dobijemo ugao za koji
												// treba zarotirati
		double xDiff1 = lastPosition.getX() - element.getPosition().getX();
		double yDiff1 = lastPosition.getY() - element.getPosition().getY();
		double alpha1 = Math.atan2(yDiff1, xDiff1);
		double xDiff2 = start.getX() - element.getPosition().getX();
		double yDiff2 = start.getY() - element.getPosition().getY();
		double alpha2 = Math.atan2(yDiff2, xDiff2);
		globalAngle += alpha1 - alpha2;
		RotateCommand newRotate = new RotateCommand(true, alpha1 - alpha2, globalAngle);
		newRotate.setDocument(context.getView());
		newRotate.setCenterOfRotation(element.getPosition());
		newRotate.setRotatedElements(context.getView().getModel().getSelectedElements());
		context.getView().getModel().getInvoker().executeCommand(newRotate);
		context.getController().setCurrentState(new SelectionState(context));
		context.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
		globalAngle = 0;
	}

	/** @param e */
	public void mouseDragged(MouseEvent e) {
		// TODO: implement
		Point2D lastPosition = e.getPoint();
		context.pointToUserSpace(lastPosition);
		double xDiff1 = lastPosition.getX() - element.getPosition().getX();
		double yDiff1 = lastPosition.getY() - element.getPosition().getY();
		double alpha1 = Math.atan2(yDiff1, xDiff1);
		double xDiff2 = start.getX() - element.getPosition().getX();
		double yDiff2 = start.getY() - element.getPosition().getY();
		double alpha2 = Math.atan2(yDiff2, xDiff2);
		globalAngle += alpha1 - alpha2;
		RotateCommand newRotate = new RotateCommand(false, alpha1 - alpha2, globalAngle);
		newRotate.setDocument(context.getView());
		newRotate.setCenterOfRotation(element.getPosition());
		newRotate.setRotatedElements(context.getView().getModel().getSelectedElements());
		context.getView().getModel().getInvoker().executeCommand(newRotate);
		start = lastPosition;
	}

	/** @param e */
	public void keyPressed(KeyEvent e) {
		// TODO: implement
		int keycode = e.getKeyCode(); 
		if (keycode == KeyEvent.VK_ESCAPE)
		{
			RotateCommand newRotate = new RotateCommand(false, -globalAngle, globalAngle);
			newRotate.setDocument(context.getView());
			newRotate.setCenterOfRotation(element.getPosition());
			newRotate.setRotatedElements(context.getView().getModel().getSelectedElements());
			context.getView().getModel().getInvoker().executeCommand(newRotate);
			context.getController().setCurrentState(new SelectionState(context));
			context.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
		// restart
			globalAngle = 0;
			RotateCommand.translateConnectionPoints=null;
		}
	}

	/** @param context */
	public RotateState(GraphWorkArea context, GraphElement element, Point2D start) {
		super(context);
		this.setStart(start);
		this.element = element;

	}

	public Point2D getStart() {
		return start;
	}

	public void setStart(Point2D start) {
		this.start = start;
	}
}